import axios from 'axios';

// 创建一个 axios 实例，用于发送请求
const account_axios = axios.create({
    // 设置后端 API 的基础 URL
    baseURL: 'http://localhost:8080/account',
    mode: "cors",
    // 设置请求头
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
});

export default{
    promise_process (promise){
        return promise.then( data => [null, data.data])
                      .catch( err => [err, null]);
    },


    async login(account){
        const response = await account_axios.post('', account)
        return response;
    },

    async logout(){
        const response = await account_axios.get('/logout')
        return response
    },

    async register(account){
        const response = await account_axios.put('/register')
        return response
    },



    async editAccount(username, account){
        const response = await account_axios.put(`/${username}/update`, account)
        return response
    },

    async getAccountByName(username){
        const response = await account_axios.get(`/${username}`)
        return response
    },

    // async getAccountById(id){
    //     const response = await axios({
    //         url             : `http://127.0.0.1:5000/users/${id}`,
    //         mode            : 'cors',
    //         method          : 'GET',     
    //         withCredentials : true,
    //     })
    // }

    async getAccounts(){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    }

}
